﻿using Coater.Domain;
using Coater.Helper;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Coater.Forms
{
    public partial class FormConfiguration : Form
    {
        public FormConfiguration()
        {
            InitializeComponent();
        }

        private void LoadConfig()
        {
            #region Program Preferences
            this.chkDemoMode.Checked = DeviceContext.Instance.Config.DemoMode;
            this.chkDisableWindowsTaskBar.Checked = DeviceContext.Instance.Config.DisableWindowsTaskBar;
            this.txtApplicationTitle.Text = DeviceContext.Instance.Config.ApplicationTitle;
            #endregion Program Preferences

            #region Machine Enables
            this.chkEnableCoater.Checked = DeviceContext.Instance.Config.EnableCoater;
            this.chkEnableReflowOven.Checked = DeviceContext.Instance.Config.EnableReflowOven;
            this.chkBatch.Checked = DeviceContext.Instance.Config.Batch;
            this.chkEnableShuttleLoader.Checked = DeviceContext.Instance.Config.EnableShuttleLoader;
            this.chkEnableSECS.Checked = DeviceContext.Instance.Config.EnableSECS;
            #endregion Machine Enables

            #region Coater

            #region Communications
            this.cmbComName_Coater.Text = DeviceContext.Instance.Config.Coater.ComPort;
            #endregion Communications

            #region Run mode
            switch (DeviceContext.Instance.Config.Coater.RunMode)
            {
                case 0:
                    {
                        this.radNormal.Checked = true;
                        this.chkDryRun.Enabled = true;
                    }; break;
                case 1:
                    {
                        this.radWaferReturn.Checked = true;
                        this.chkDryRun.Enabled = false;
                    }; break;
            }
            this.chkDryRun.Checked = DeviceContext.Instance.Config.Coater.DryRun;
            #endregion Run mode

            #region Maintenance
            this.lblCycles.Text = DeviceContext.Instance.Config.Coater.Cycles.ToString();
            this.lblInterval.Text = DeviceContext.Instance.Config.Coater.Interval.ToString();
            this.lblTotal.Text = DeviceContext.Instance.Config.Coater.Total.ToString();
            #endregion Maintenance

            #region handling
            this.chkRobotLoadOptionPresent.Checked = DeviceContext.Instance.Config.Coater.RobotLoadOptionPresent;
            this.chkEdgeChuckOptionPresent.Checked = DeviceContext.Instance.Config.Coater.EdgeChuckOptionPresent;
            #endregion  handling

            #region Preferences
            this.chkUseSMEMASignals.Checked = DeviceContext.Instance.Config.Coater.UseSMEMASignals;
            this.chkAutoWaferReady.Checked = DeviceContext.Instance.Config.Coater.AutoWaferReady;
            this.chkUseAsystNetworkInterface.Checked = DeviceContext.Instance.Config.Coater.UseAsystNetworkInterface;
            this.chkDefeatLidInterlock.Checked = DeviceContext.Instance.Config.Coater.DefeatLidInterlock;
            this.chkSlowSequence.Checked = DeviceContext.Instance.Config.Coater.SlowSequence;
            this.chkUseVacuumBeforeCentering.Checked = DeviceContext.Instance.Config.Coater.UseVacuumBeforeCentering;
            this.chkBackupToEntrySensorInWaferReturnMode.Checked = DeviceContext.Instance.Config.Coater.BackupToEntrySensorInWaferReturnMode;
            this.numWaferVacuumDelay.Value = DeviceContext.Instance.Config.Coater.WaferVacuumDelay;
            this.chkEnableWafer8_12SelectionDigitalOutput.Checked = DeviceContext.Instance.Config.Coater.EnableWafer8_12SelectionDigitalOutput;
            this.numWaferPassThroughDelay.Value = DeviceContext.Instance.Config.Coater.WaferPassThroughDelay;
            this.chkUseWaferEntryDelay.Checked = DeviceContext.Instance.Config.Coater.UseWaferEntryDelay;
            this.numWaferEntryDelay.Value = DeviceContext.Instance.Config.Coater.WaferEntryDelay;
            this.chkEnableAudibleAlarmOff.Checked = DeviceContext.Instance.Config.Coater.EnableAudibleAlarmOff;
            this.chkCheckUSAtCenter.Checked = DeviceContext.Instance.Config.Coater.CheckUSAtCenter;
            this.chkPartitionBelts.Checked = DeviceContext.Instance.Config.Coater.PartitionBelts;
            this.chkEnableBacksideNozzle6.Checked = DeviceContext.Instance.Config.Coater.EnableBacksideNozzle6;
            this.chkEnableOffSweep.Checked = DeviceContext.Instance.Config.Coater.EnableOffSweep;
            #endregion Preferences

            #endregion Coater

            #region Recipe storage path
            this.txtRecipePath.Text = DeviceContext.Instance.Config.RecipePath;
            #endregion Recipe storage path
        }

        private void FormConfiguration_Load(object sender, EventArgs e)
        {
            LoadConfig();
        }

        private void cmbCom_DropDown(object sender, EventArgs e)
        {
            try
            {
                cmbComName_Coater.Items.Clear();
                var ports = SerialPort.GetPortNames().OrderBy(a => a.ToString());
                foreach (string port in ports)
                {
                    this.cmbComName_Coater.Items.Add(port);
                }
            }
            catch (Exception ex)
            {
                LogHelper.ErrorLog.Error(ex);
            }
        }

        private void btnRecipeOpenPath_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog ofd = new FolderBrowserDialog();
            ofd.InitialDirectory = this.txtRecipePath.Text;
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                this.txtRecipePath.Text = ofd.SelectedPath;
            }
        }

        private void chkEnableReflowOven_CheckedChanged(object sender, EventArgs e)
        {
            if (this.chkEnableReflowOven.Checked)
            {
                this.chkBatch.Enabled = true;
            }
            else
            {
                this.chkBatch.Checked = false;
                this.chkBatch.Enabled = false;
            }
        }

        private void radNormal_CheckedChanged(object sender, EventArgs e)
        {
            this.chkDryRun.Enabled = this.radNormal.Checked;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            Configuration config = new Configuration();
            #region Program Preferences
            config.DemoMode = this.chkDemoMode.Checked;
            config.DisableWindowsTaskBar = this.chkDisableWindowsTaskBar.Checked;
            config.ApplicationTitle = this.txtApplicationTitle.Text;
            #endregion Program Preferences

            #region Machine Enables
            config.EnableCoater = this.chkEnableCoater.Checked;
            config.EnableReflowOven = this.chkEnableReflowOven.Checked;
            config.Batch = this.chkBatch.Checked;
            config.EnableShuttleLoader = this.chkEnableShuttleLoader.Checked;
            config.EnableSECS = this.chkEnableSECS.Checked;
            #endregion Machine Enables

            #region Coater

            #region Communications
            config.Coater.ComPort = this.cmbComName_Coater.Text;
            #endregion Communications

            #region Run mode
            config.Coater.DryRun = this.chkDryRun.Checked;
            if (radNormal.Checked)
                config.Coater.RunMode = 0;
            else if (radWaferReturn.Checked)
                config.Coater.RunMode = 1;
            #endregion Run mode

            #region Maintenance
            config.Coater.Cycles = Convert.ToInt32(this.lblCycles.Text);
            config.Coater.Interval = Convert.ToInt32(this.lblInterval.Text);
            config.Coater.Total = Convert.ToInt32(this.lblTotal.Text);
            #endregion Maintenance

            #region handling
            config.Coater.RobotLoadOptionPresent = this.chkRobotLoadOptionPresent.Checked;
            config.Coater.EdgeChuckOptionPresent = this.chkEdgeChuckOptionPresent.Checked;
            #endregion  handling

            #region Preferences
            config.Coater.UseSMEMASignals = this.chkUseSMEMASignals.Checked;
            config.Coater.AutoWaferReady = this.chkAutoWaferReady.Checked;
            config.Coater.UseAsystNetworkInterface = this.chkUseAsystNetworkInterface.Checked;
            config.Coater.DefeatLidInterlock = this.chkDefeatLidInterlock.Checked;
            config.Coater.SlowSequence = this.chkSlowSequence.Checked;
            config.Coater.UseVacuumBeforeCentering = this.chkUseVacuumBeforeCentering.Checked;
            config.Coater.BackupToEntrySensorInWaferReturnMode = this.chkBackupToEntrySensorInWaferReturnMode.Checked;
            config.Coater.WaferVacuumDelay = (int)this.numWaferVacuumDelay.Value;
            config.Coater.EnableWafer8_12SelectionDigitalOutput = this.chkEnableWafer8_12SelectionDigitalOutput.Checked;
            config.Coater.WaferPassThroughDelay = (int)this.numWaferPassThroughDelay.Value;
            config.Coater.UseWaferEntryDelay = this.chkUseWaferEntryDelay.Checked;
            config.Coater.WaferEntryDelay = (int)this.numWaferEntryDelay.Value;
            config.Coater.EnableAudibleAlarmOff = this.chkEnableAudibleAlarmOff.Checked;
            config.Coater.CheckUSAtCenter = this.chkCheckUSAtCenter.Checked;
            config.Coater.PartitionBelts = this.chkPartitionBelts.Checked;
            config.Coater.EnableBacksideNozzle6 = this.chkEnableBacksideNozzle6.Checked;
            config.Coater.EnableOffSweep = this.chkEnableOffSweep.Checked;
            #endregion Preferences

            #endregion Coater

            #region Recipe storage path
            config.RecipePath = this.txtRecipePath.Text.Trim();
            #endregion Recipe storage path

            DeviceContext.Instance.SaveConfig(config);

            DialogResult = DialogResult.OK;
        }
    }
}


